<div class="layuimini-main">
	<div style="margin: 5px 5px 5px 5px">
		<form class="layui-form layui-form-pane" action="">
			<div class="layui-form-item">
				<div class="layui-inline">
					<div class="layui-input-inline">
						<input type="text" name="namespaceName" autocomplete="off" class="layui-input" placeholder="命名空间名称">
					</div>
				</div>
				<div class="layui-inline">
					<button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="namespace-data-search-btn">搜 索</button>
				</div>
			</div>
		</form>
	</div>
	<script type="text/html" id="namespace-toolbarDemo">
		<div class="layui-btn-container">
			<button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="namespace-add"> 添加 </button>
		</div>
	</script>

	<table class="layui-hide" id="namespaceTableId" lay-filter="namespaceTableFilter"></table>

	<script type="text/html" id="namespaceTableBar">
		<a href="javascript:;" class="layui-font-blue" lay-event="namespace-delete">删除</a>
	</script>
</div>
<script>

	//全局变量，在子页面里使用
	var parentData2;
	
    layui.use(['form', 'table', 'miniPage'], function () {
        var $ = layui.jquery,
            form2 = layui.form,
            table2 = layui.table,
			miniPage2 = layui.miniPage;
		var clusterId = JSON.parse(parent.parentData).id;
			
        table2.render({
			id: 'namespaceTableId',
            elem: '#namespaceTableId',
            url: '/cluster/namespace/page',
			method: 'post',
			contentType: 'application/json;charset=UTF-8',
			request: {
				pageName: 'pageNum',
				limitName: 'pageSize'
			},
			where: {
				'clusterId': clusterId
			},
            toolbar: '#namespace-toolbarDemo',
            defaultToolbar: ['filter'],
			parseData: function(res){
				redirectLogin(res.code);
				if(res.code != '000000'){
					return {
					  "code": 1,
					  "msg": res.message,
					  "count": 0,
					  "data": []
					};
				}
				if(res.code != '000000'){
					return {
					  "code": 1,
					  "msg": res.message,
					  "count": 0,
					  "data": []
					};
				}
				return {
				  "code": 0,
				  "msg": res.message,
				  "count": res.data.itemCount,
				  "data": res.data.items
				};
			},
            cols: [[
                {field: 'namespaceName', title: '命名空间名称'},
                {title: '操作', toolbar: '#namespaceTableBar'}
            ]],
            limits: [10, 20],
            limit: 20,
            page: true,
            skin: 'row',
			even: true
        });

        // 监听搜索操作
        form2.on('submit(namespace-data-search-btn)', function (data) {
            table2.reload('namespaceTableId', {
                page: {
                    curr: 1
                },
                where: {
					'namespaceName': data.field.namespaceName,
					'clusterId': clusterId
				}
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table2.on('toolbar(namespaceTableFilter)', function (obj) {
			parentData2 = JSON.stringify({'clusterId' : clusterId});
            if (obj.event === 'namespace-add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加命名空间',
                    type: 1,
                    shade: 0.2,
                    maxmin: false,
                    shadeClose: false,
                    area: ['30%', '25%'],
                    offset: ['30%', '32%'],
					content: miniPage2.getHrefContent('page/cluster/namespace/add.html')
                });
            }
        });

        table2.on('tool(namespaceTableFilter)', function (obj) {
			if (obj.event === 'namespace-delete') {
                doDelete($, clusterId, obj.data.namespaceName);
            }
			return false;
        });
    });
	
	//删除
	function doDelete($, clusterId, namespaceName) {
		var msg = "同时会删除" + namespaceName + "下的所有资源，确定删除吗？";
		layer.confirm(msg, {icon: 0, title: '警告提示'}, function (index) {
			layer.close(index);
			$.ajax({
				url: '/cluster/namespace/delete',
				type: 'POST',
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify({"clusterId": clusterId, "namespaceName": namespaceName}),
				success: function(data){
					if(data.code != "000000"){
						layer.msg(data.message, {icon: 5, shift: 6});
						return false;
					}
					
					layer.msg("删除成功", {
						icon: 1,
						time: 700,
						shade: 0.01,
						shadeClose: false}, function(){
						layui.table.reload('namespaceTableId');
					});
				},
				error: function(data){
					layer.msg(data.message, {icon: 5, shift: 6});
				}
			});
		});
	}
</script>